package com.huaju.enity;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import static org.hibernate.criterion.Example.create;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * A data access object (DAO) providing persistence and search support for Wages
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.huaju.enity.Wages
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class WagesDAO {
	private static final Logger log = LoggerFactory.getLogger(WagesDAO.class);
	// property constants
	public static final String WDESC = "wdesc";
	public static final String WNUM = "wnum";
	public static final String BASENUM = "basenum";
	public static final String COUNUM = "counum";
	public static final String ETR = "etr";

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	protected void initDao() {
		// do nothing
	}

	public void save(Wages transientInstance) {
		log.debug("saving Wages instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void saveOrUpdate(Wages transientInstance) {
		log.debug("saving Wages instance");
		try {
			getCurrentSession().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Wages persistentInstance) {
		log.debug("deleting Wages instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Wages findById(java.lang.Integer id) {
		log.debug("getting Wages instance with id: " + id);
		try {
			Wages instance = (Wages) getCurrentSession().get(
					"com.huaju.enity.Wages", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Wages> findByExample(Wages instance) {
		log.debug("finding Wages instance by example");
		try {
			List<Wages> results = (List<Wages>) getCurrentSession()
					.createCriteria("com.huaju.enity.Wages")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Wages instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Wages as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public Integer countAll(String pro, Object value) {
		log.debug("finding all bankcard instances");
		try {
			String queryString = "from Wages model where model." + pro + "=?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByPage(Integer start, Integer size, String pro, Object value) {
		log.debug("finding all bankcard instances");
		try {
			String queryString = "from Wages model where model." + pro + "=? order by status asc,wtime desc";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByAccountAndMonth(Account user, String desc) {
		try {
			String queryString = "from Wages as model where model.account= ? and model.wdesc=?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, user);
			queryObject.setParameter(1, desc);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByStatusProperty(String propertyName, Object value,
			Integer status) {
		log.debug("finding Wages instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Wages as model where model."
					+ propertyName + "= ? and model.status=? order by wid desc," ;
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryObject.setParameter(1, status);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAnalysis(Integer comId) {
		try {
			String queryString = "select count(*),sum(wnum) from wages where status=0";
			if (comId != null) {
				queryString += " and comId="+ comId;
			}
			SQLQuery queryObject = getCurrentSession().createSQLQuery(
					queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	
	
	

	public List<Wages> findByWdesc(Object wdesc) {
		return findByProperty(WDESC, wdesc);
	}

	public List<Wages> findByWnum(Object wnum) {
		return findByProperty(WNUM, wnum);
	}

	public List<Wages> findByBasenum(Object basenum) {
		return findByProperty(BASENUM, basenum);
	}

	public List<Wages> findByCounum(Object counum) {
		return findByProperty(COUNUM, counum);
	}

	public List<Wages> findByEtr(Object etr) {
		return findByProperty(ETR, etr);
	}

	public List findAll() {
		log.debug("finding all Wages instances");
		try {
			String queryString = "from Wages";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Integer countAll() {
		log.debug("finding all Wages instances");
		try {
			String queryString = "from Wages";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByPage(Integer start, Integer size) {
		log.debug("finding all Wages instances");
		try {
			String queryString = "from Wages order by status asc,wtime desc";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Integer countByProAll(String propertyName, Object value) {
		log.debug("finding all Wages instances");
		try {
			String queryString = "from Wages as model where model."
					+ propertyName + "= ?";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Integer countByProAll(Map<String, String> parameters,
			String propertyName, String value) {
		log.debug("finding all Bankcard instances");
		try {

			String queryString = "from Wages as model where ";
			for (Entry<String, String> entry : parameters.entrySet()) {
				queryString += " model." + entry.getKey() + "="
						+ String.format("%s", entry.getValue()) + " and ";
			}

			queryString += " model." + propertyName + " like:sname";

			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter("sname", "%" + value + "%");
			return queryObject.list().size();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByProPage(Map<String, String> parameters, Integer start,
			Integer size, String propertyName, Object value) {
		log.debug("finding all Wages instances");
		try {
			String queryString = "from Wages as model where ";
			for (Entry<String, String> entry : parameters.entrySet()) {
				queryString += " model." + entry.getKey() + "="
						+ String.format("%s", entry.getValue()) + " and ";
			}

			queryString += " model." + propertyName + " like:sname order by  wtime desc ,status asc";
			Query queryObject = getCurrentSession().createQuery(queryString);
			queryObject.setParameter("sname", "%" + value + "%");
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByProPage(Integer start, Integer size, String propertyName,
			Object value) {
		log.debug("finding all Wages instances");
		try {
			String queryString = "select * from wages as model where model."
					+ propertyName + " like :sname";
			SQLQuery queryObject = getCurrentSession().createSQLQuery(
					queryString).addEntity(Wages.class);
			queryObject.setParameter("sname", "%" + value + "%");
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByProPages(Integer start, Integer size,
			String propertyName, Object value) {
		log.debug("finding all Wages instances");
		try {
			String queryString = "select * from wages as model where model."
					+ propertyName + " like :sname";
			SQLQuery queryObject = getCurrentSession().createSQLQuery(
					queryString).addEntity(Wages.class);
			queryObject.setParameter("sname", "%" + value + "%");
			queryObject.setMaxResults(size);
			queryObject.setFirstResult(start);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Wages merge(Wages detachedInstance) {
		log.debug("merging Wages instance");
		try {
			Wages result = (Wages) getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Wages instance) {
		log.debug("attaching dirty Wages instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Wages instance) {
		log.debug("attaching clean Wages instance");
		try {
			getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
					instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static WagesDAO getFromApplicationContext(ApplicationContext ctx) {
		return (WagesDAO) ctx.getBean("WagesDAO");
	}
}